package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.DirectionDao;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.dto.CommonQueryDto;
import com.tpyule.tpclass.entity.dto.CourseParamDto;
import com.tpyule.tpclass.entity.pojo.Direction;
import com.tpyule.tpclass.service.CourseService;
import com.tpyule.tpclass.service.DirectionService;
import com.tpyule.tpclass.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zzz
 * @date 2020/2/26 0026 下午 3:12
 */
@Service
public class DirectionServiceImpl implements DirectionService {
    @Autowired
    DirectionDao directionDao;
    @Autowired
    SortService sortService;
    @Autowired
    CourseService courseService;

    @Override
    public int addDirection(Direction d) {
        return directionDao.addDirection(d);
    }

    @Override
    public List<Direction> getAllDirection() {
        List<Direction> directions=directionDao.getAllDirection();
        return directions;
    }

    @Override
    public Direction getSortByDirectionId(int sortId) {
        return directionDao.getDirectionBySortId(sortId);
    }

    @Override
    public int getSizeDirection() {
        return directionDao.getSizeDirection();
    }

    @Override
    public List<Direction> getListDirection(CommonQueryDto commonQueryDto) {
        commonQueryDto.setStart((commonQueryDto.getPage()-1)*commonQueryDto.getPageSize());
        return directionDao.getListDirection(commonQueryDto);
    }

    @Override
    public int updateDirection(Direction direction) {
        return directionDao.updateDirection(direction);
    }

    @Override
    public ResultBean deleteDirection(Direction direction) {
        ResultBean resultBean = null;
        if (direction!=null){
            int sortSize = sortService.getSizeSortByDirection(direction.getDirectionId());
            if(sortSize!=0){
                resultBean = new FailResultBean(501,"该方向下还有分类");
            }else {
                CourseParamDto courseParamDto = new CourseParamDto();
                courseParamDto.setDirectionId(direction.getDirectionId());
                int courseSize = courseService.getSizeCourse(courseParamDto);
                if(courseSize!=0){
                    resultBean = new FailResultBean(502,"该学习方向下还有课程");
                }else{
                    int result = directionDao.deleteDirection(direction);
                    if(result>0){
                        resultBean = new SuccessResultBean();
                    } else {
                        resultBean = new FailResultBean("删除失败，请重试！");
                    }
                }
            }
        } else {
            resultBean = new FailResultBean("数据错误");
        }
        return resultBean;
    }
}
